/**
 * Created with IntelliJ IDEA
 * Description:
 * User:恋恋
 * Date:2022-04-13
 * Time:21:43
 */
class Money implements Cloneable{
    public double money=19.99;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();//重写父类的克隆
        //super就是Object--Money
    }
}
class People implements Cloneable{//空接口
    int sore;//普通的成员变量
    //创建另一个类的对象，成为这个类的成员变量
    public Money m=new Money();

    @Override
    public String toString() {
        return "People{" +
                "sore=" + sore +
                '}';
    }
//Object类是所有类的父类
    @Override
    protected Object clone() throws CloneNotSupportedException {//throws CloneNotSupportedException--处理异常
       // return super.clone();
        People tem=(People)super.clone();
        tem.m=(Money)this.m.clone();//深拷贝的关键在于对象中的成员也是个对象，实现深拷贝是代码层次上进行的，不是说某个方法是深拷贝
        //那个对象需要拷贝，就需要在定义对象的类中重写Clonable接口的clone方法，并且在需要拷贝的方法名中添加声明处理异常throws CloneNotSupportedException
        return tem;//强行转化类型---Object类
    }
}
public class TestDome {
    public static void main(String[] args) throws CloneNotSupportedException{//需在main函数中声明处理异常
        People people1=new People();
        //浅拷贝--将people1中的成员在堆上拷贝一份
        People people2=(People) people1.clone();//必须强制转换Object类，且Object类是所有类的父类
        System.out.println("+++++++   浅拷贝   ++++++++");
        System.out.println(people1.m.money);
        System.out.println(people2.m.money);
        System.out.println("+++++++   深拷贝   ++++++++");
        people2.m.money=99.9;
        System.out.println(people1.m.money);
        System.out.println(people2.m.money);
    }

}
